Alist in emacs-lisp
An association list or alist for short is a data type in emacs-lisp which maps key to values. It is a list of cons cells called associations. The CAR of each cons cell is the "key" and the CDR is the "value".
Here is an example of an alist. The key is a person's 'name' associated with 'location'.
(setq people '((John . Bengaluru) (Mike . Delhi) (Rita . Mumbai))) ;; Let's iterate over the alist, Remember CAR of cons is key and CDR is value (dolist (person people) (print (format "%s from %s" (car person) (cdr person))))
Let's see some more functions which we generally use with alist
(setq people '((John . Bengaluru) (Mike . Delhi) (Rita . Mumbai))) (assoc 'John people) ; (John . Bengaluru) (assoc 'Ram people) ; nil (cdr (assoc 'John people)) ; Bengaluru ;; Get alist from value, it'll return only first one (rassoc 'Bengaluru people) ; (John . Bengaluru) ;; Delete alist from key (setq people (assq-delete-all 'John people)) (assoc 'John people) ; nil